# -*- coding: utf-8 -*-
import scrapy
import json
import re


class MoguSpider(scrapy.Spider):
    name = 'mogu'
    allowed_domains = ['mogu.com']
    start_urls = ['https://mce.mogucdn.com/jsonp/get/3']

    def start_requests(self):
        url = 'https://mce.mogucdn.com/jsonp/get/3'
        data = {
            "pid": "133798",
            "callback": "httpCb157495369271454",
            "_": "1574953692714",
        }
        yield scrapy.FormRequest(url=url, formdata=data, method='GET', callback=self.parse)

    def parse(self, response):
        text = response.text
        text_json = re.search('\((.*)\)', text).group(1)
        data_dic = json.loads(text_json)
        data_list = data_dic['data']['list']
        url = 'https://shop.mogu.com/detail/1lsuvke'
        for data in data_list:
            formdata = {'acm': data['acm']}
            yield scrapy.FormRequest(url=url, formdata=formdata, method='GET', callback=self.parsebody)

    def parsebody(self, response):
        print(response.text)
